<#include '/admin/header.html' >
<div class="main">
	<div class='buttonArea'>
		<a href="javascript:void(0)" class="easyui-linkbutton" onclick="append()">添加</a>
		<!-- 批量删除暂时注释掉 DMRain 2016-5-30
		<a href="javascript:void(0)" class="easyui-linkbutton" onclick="del()">删除</a>
		 -->
	</div>
	
	<form id="bonusform">
		<div class="shadowBoxWhite tableDiv">
			<table class="easyui-datagrid"
				data-options="url:'${ctx}/shop/admin/bonus-type/list-json.do',pageList: [10,15,19,30],pageSize:${pageSize},fitColumns:'true'"
				pagination="true" id="bonusdata">
				<thead>
					<tr>
						<th data-options="field:'type_id',checkbox:true,width:50" ></th>
						<th data-options="field:'type_name',width:50">类型名称</th>
						<th data-options="field:'send_type',width:100" formatter="formatsendtype">发放类型</th>
						<th data-options="field:'type_money',width:50">红包金额</th>
						<th data-options="field:'min_goods_amount',width:50">订单下限</th>
						<th data-options="field:'create_num',width:70">发放数量</th>
						<th data-options="field:'use_num',width:50">使用数量</th>
						<th data-options="field:'use_start_date',width:70" formatter="formatDate">使用开始时间</th>
						<th data-options="field:'use_end_date',width:70" formatter="formatDate">使用结束时间</th>
						<th data-options="field:'action',width:100" formatter="formatOperation">操作</th>
					</tr>
				</thead>
			</table>
		</div>
	</form>
	
	<div id="divdia" style="display: none;"></div>
	
</div>
<script type="text/javascript">
	function append(id) {
		var map = {}; // Map map = new HashMap();
		if (!id) {
			map["href"] = "${ctx}/shop/admin/bonus-type/add.do";
			map["formId"] = "#bonusTypeForm";
			map["url"] = "${ctx}/shop/admin/bonus-type/save-add.do?ajax=yes";
			map["title"] = "添加红包类型";
			map["loadshow"] = "正在添加......";
		} else {
			map["href"] = "${ctx}/shop/admin/bonus-type/edit.do?typeid=" + id;
			map["formId"] = "#editBonusTypeForm";
			map["url"] = "${ctx}/shop/admin/bonus-type/save-edit.do?ajax=yes";
			map["title"] = "修改红包类型";
			map["loadshow"] = "正在修改......";
		}
		map["divDialog"] = "#divdia";
		map["gridreload"] = "#bonusdata";

		addDialog(map);
	}
	function addDialog(map) {
		$(map["divDialog"]).show();
		$(map["divDialog"]).dialog({
			title : map["title"],
			width : 750,
			height : 400,
			closed : false,
			cache : false,
			href : map["href"],
			modal : true,
			buttons : [ {
				text : '保存',
				iconCls : 'icon-ok',
				handler : function() {
					submitForm(map);
				}
			}, {
				text : '取消',
				handler : function() {
					$(map["divDialog"]).dialog('close');
				}
			} ]
		});
	}
	function submitForm(map) {
		var formflag = $(map["formId"]).form().form('validate');
		if (formflag) {
			$.Loading.show(map["loadshow"]);
			var options = {
				url : map["url"],
				type : "POST",
				dataType : 'json',
				success : function(result) {
					if (result.result == 1) {
						$(map["divDialog"]).dialog('close');
						$(map["gridreload"]).datagrid('reload');
						$.Loading.success(result.message);
					}
					if (result.result == 0) {
						$.Loading.error(result.message);
					}
				},
				error : function(e) {
					alert("出现错误 ，请重试");
				}
			};
			$(map["formId"]).ajaxSubmit(options);
		}
	}

	function clearForm(map) {
		$(map["formId"]).form('clear');
	}
	
	function removeBonus(typeid){
		
		if (!confirm("确认要将此优惠劵删除吗？")) {
			return;
		}
		
		$(".delete").removeAttr("onclick");
		$.ajax({
			url:"${ctx}/shop/admin/bonus-type/delete.do?type_id="+typeid+"&ajax=yes",
			dataType:"json",
			success:function(res){
				if(res.result==1){
					$.Loading.success(res.message);
					$('#bonusdata').datagrid("reload");
				}else{
					$.Loading.error(res.message);
				}
				$(".delete").attr("onclick","removeBonus("+typeid+")");
			},
			error:function(){
				alert("删除出现意外错误");
				$(".delete").attr("onclick","removeBonus("+typeid+")");
			}
		});
	}

	function formatOperation(value, row, index) {
		var val = "";
		val += "<a class='issue' title='发放' href='javascript:void(0);' onclick='newTab(\"发放红包\",\"${ctx}/shop/admin/bonus/send.do?typeid="
				+ row.type_id + "\")'></a>"
		val += "<a class='view' title='查看' href='javascript:void(0);' onclick='newTab(\"红包列表\",\"${ctx}/shop/admin/bonus/list.do?typeid="
				+ row.type_id + "&send_type="+row.send_type+"\")' ></a>"
		if(parseInt(row.can_be_edit)!=1){
			val += "<a class='edit' title='编辑' href='javascript:void(0);' onclick='append("
				+ row.type_id + ")'></a>" 
			val += "<a class='delete' title='移除' href='javascript:void(0);' onclick='removeBonus("+row.type_id+")' ></a>"
		}
		
	/* 	if (row.send_type == 3){
			val += "<a class='edit' title='报表' href='bonus!exportExcel.do?typeid="+row.type_id+"&ajax=yes' ></a>"
		} */
		return val;
	}
	
	//报表打印  xulipeng 
	function exportExc(tid){
	}
	
	//批量删除暂时注释掉 DMRain 2016-5-30
	/* function del() {
		var rows = $('#bonusdata').datagrid("getSelections");
		if (rows.length < 1) {
			$.Loading.error('请选择要删除的优惠劵');
			return;
		}
		if (!confirm("确认要将这些优惠劵删除吗？")) {
			return;
		}
		var options = {
			url : "${ctx}/shop/admin/bonus-type/delete.do?ajax=yes",
			type : "POST",
			dataType : 'json',
			success : function(result) {
				if (result.result == 1) {
					$.Loading.success('删除成功');
					$('#bonusdata').datagrid('reload');
				}
				if (result.result == 0) {
					$.Loading.error(result.message);
				}
			},
			error : function(e) {
				$.Loading.error("出现错误，请重试");
			}
		};
		$('#bonusform').ajaxSubmit(options);
	} */

	function formatsendtype(value, row, index) {
		var val;
		if (value == 0) {
			val = "按用户发放";
		}
		if (value == 1) {
			val = "按商品发放";
		}
		if (value == 2) {
			val = "按订单金额发放";
		}
		if (value == 3) {
			val = "线下发放的红包";
		}
		return val;
	}

	//获取今天的日期
	$(function() {
		var curr_time = new Date();
		var strDate = curr_time.getFullYear() + "-";
		strDate += curr_time.getMonth() + 1 + "-";
		strDate += curr_time.getDate() + "-";
		strDate += curr_time.getHours() + ":";
		strDate += curr_time.getMinutes() + ":";
		strDate += curr_time.getSeconds();
	});
	function formatDate(value,row,index){
		if(value==null||value==0){
			return "";
		}
		else{
			return getFormatDateByLong(value, "yyyy-MM-dd");
		}
		
	}
</script>
<#include '/admin/footer.html' >
